package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.service.IXibService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 报送任务结束批量
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class XibFileEndConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(XibFileEndConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IXibService xibService;

    @Bean
    public Job xibFileEndJob(){
        return jobBuilderFactory.get("xibFileEndJob")
                .start(xibFileEndStep())
                .build();
    }

    @Bean
    public Step xibFileEndStep() {
        return stepBuilderFactory.get("xibFileEndStep")
                .tasklet(xibFileEndStepTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet xibFileEndStepTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.debug("报送任务结束批量,开始。。。。。。");

            xibService.createXibEndLog();

            logger.debug("报送任务结束批量处理完成");
            return RepeatStatus.FINISHED;
        };
    }
}
